package org.belkodevelop.registration.dto;

import org.belkodevelop.controller.json.ErrorItem;
import org.springframework.context.MessageSource;
import org.springframework.validation.FieldError;

import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * @author VMozolev
 */
public class RegistrationStatus {
    private boolean error;
    private List<ErrorItem> errors;

    private static List<ErrorItem> convertListObjectErrorToListErrorItem(List<FieldError> fieldErrors, Locale locale, MessageSource messageSource) {
        List<ErrorItem> list = new LinkedList<ErrorItem>();
        for (FieldError oe: fieldErrors) {
            list.add(new ErrorItem(oe, locale, messageSource));
        }
        return list;
    }

    public RegistrationStatus(boolean error) {
        this.error = error;
    }

    public RegistrationStatus(boolean error, List<FieldError> fieldErrors, Locale locale, MessageSource messageSource) {
        this.error = error;
        this.errors = convertListObjectErrorToListErrorItem(fieldErrors, locale, messageSource);
    }

    public boolean isError() {
        return error;
    }

    public List<ErrorItem> getErrors() {
        return errors;
    }
}